from threading import Thread, BoundedSemaphore
from requests import get

result = []
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36'}


def check(key, url, lock):
    with lock:
        urls = url + key
        try:
            res = get(urls, headers=headers, timeout=2)
            if res.status_code == 200:
                print(urls)
                result.append(urls)
        except Exception as e:
            return result
    return result


def run(url):
    semaphore = BoundedSemaphore(50)
    threads_list = []
    f = open('../dict/DIR.txt', 'r', encoding='utf-8')
    for i in f:
        key = i.strip('\n')
        t = Thread(target=check, args=(str(key), url, semaphore))
        threads_list.append(t)

    for t in threads_list:
        t.start()

    for t in threads_list:
        t.join()

    if not result:
        result.append(f'输入错误或目标不在线，输入示例http://www.scitc.com.cn')
        return result
    else:
        return result
